package TwentyOne.Class.Map;
/* 
数据表与简单Java类映射转换

数据实体表设计 = 类的定义；
表中的字段 = 类的成员属性；
表的外键关联 = 引用关联；
表的一行记录 = 类的一个实例化对象；
表的多行记录 = 对象数组。 
 */

/*
 部门：部门编号、部门名称、部门位置
 雇员：雇员编号、部门编号、姓名、职位、基本工资、佣金、领导编号
*/
class Dept {

    private long deptno;
    private String dname;
    private String location;
    private Emp emps[];    // 多个雇员信息

    public Dept(long deptno, String dname, String location) {
        this.deptno = deptno;
        this.dname = dname;
        this.location = location;
    }

    public void setEmps(Emp[] emps) {
        this.emps = emps;
    }

    public Emp[] getEmps() {
        return this.emps;
    }

    public void setDeptno(long deptno) {
        this.deptno = deptno;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public long getDeptno() {
        return this.deptno;
    }

    public String getDname() {
        return this.dname;
    }

    public String getLocation() {
        return this.location;
    }

    public String getInfo() {
        return "【部门信息】部门编号 = " + this.deptno + "、部门名称 = " + this.dname + "、部门位置 = " + this.location;
    }
}

class Emp {

    private long empno;
    private String ename;
    private String job;
    private double sal;
    private double comm;
    private Dept dept;   //所属部门
    private Emp mgr;     //所属领导

    public Emp(long empno, String ename, String job, double sal, double comm) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.sal = sal;
        this.comm = comm;
    }

    public String getInfo() {
        return "【雇员信息】雇员编号 = " + this.empno + "、雇员姓名 = " + this.ename + "、雇员职位 = " + this.job + "、基本工资 = " + this.sal + "、佣金 = " + this.comm;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public void setMgr(Emp mgr) {
        this.mgr = mgr;
    }

    public Dept getDept() {
        return this.dept;
    }

    public Emp getMgr() {
        return this.mgr;
    }
}

public class a10_Map {

    public static void main(String[] args) {
        //根据关系进行类的定义，定义出各个的实例对象
        Dept dept = new Dept(10,"财务部", "上海");
        Emp empA = new Emp(7369, "A1", "B1", 800.00, 0.0);
        Emp empB = new Emp(7566, "A2", "B2", 2450.00, 0.0);
        Emp empC = new Emp(7839, "A3", "B3", 5000.00, 0.0);
        empA.setDept(dept);
        empB.setDept(dept);
        empC.setDept(dept);
        empA.setMgr(empB);
        empB.setMgr(empC);
        dept.setEmps(new Emp[]{empA, empB, empC});

        // 根据关系获取数据
        System.out.println(dept.getInfo());
        for (int x = 0; x < dept.getEmps().length; x++) {
            System.out.println("\t | - " + dept.getEmps()[x].getInfo()
            );
            if (dept.getEmps()[x].getMgr() != null) {
                System.out.println("\t\t | - " + dept.getEmps()[x].getMgr().getInfo()
                );
            }
        }
        System.out.println("--------------------------------");
        System.out.println(empB.getDept().getInfo()) ; //根据雇员获取部门信息
 
        System.out.println(empB.getMgr().getInfo()) ; //根据雇员获取领导信息
    }
}